<?php
	header("Content-type:text/x-json; charset=UTF-8"); 
	header("Cache-Control: no-store, no-cache, must-revalidate"); 
	header("Cache-Control: post-check=0, pre-check=0", false); 

	$objConnect = oci_connect("hospital", "hospital", "//localhost/XE"); 
        
        $a=1;
	$page = $_POST['page']; 
	$rp = $_POST['rp']; 
	$sortname = $_POST['sortname']; 
	$sortorder = $_POST['sortorder']; 
        
        if($_POST['qtype']=='STF_ID'){
            if(!ctype_alnum($_POST["query"])) {
                    $where ='';
                    $a=1;
            }else{
                if(!ctype_alnum($_POST["query"])) {
                    $where ='';
                    $a=1;
                }else{
                    if(!ctype_digit($_POST["query"])) {
                        $where ='';
                        $a=0;
                    }else {
                        $where ="where STF_ID = ".$_POST['query'];
                    }
                }
            }
        }
        
        
        
        if($_POST['qtype']=='JOB_TYPE'){
            if(!ctype_alnum($_POST["query"])) {
                    $where ='';
                    $a=1;
            }else{
                if(!ctype_alnum($_POST["query"])) {
                    $where ='';
                    $a=1;
                }else{
                    if(!ctype_digit($_POST["query"])) {
                        $where ="WHERE JOB_TYPE LIKE '%".$_POST['query']."%'";
                   
                    }else {
                        $where ='';
                        $a=0;
                    }
                }
            }
        }
        
	if (!$sortname) $sortname = 'STF_ID'; 
	if (!$sortorder) $sortorder = 'ASC'; 
	if (!$page) $page = 1; 
	if (!$rp) $rp = 10; 

        if($_POST['query']=='')$start = (($page-1) * $rp);
        else $start=0;
        
	//$start = (($page-1) * $rp);
	$limit = "LIMIT $start, $rp";
	$sort = "ORDER BY $sortname $sortorder";

	$strSQL = "SELECT * FROM STAFF $where $sort";
	$objParse = oci_parse($objConnect, $strSQL);
	oci_execute ($objParse,OCI_DEFAULT);

	$total = oci_fetch_all($objParse, $Result);

        if($total!=0&&$a==1){
            $strSQL = "SELECT STF_ID,F_NAME,L_NAME,JOB_TYPE,SALARY,START_CONTACT,END_CONTACT
                       FROM ( 
                            SELECT STF_ID,F_NAME,L_NAME,JOB_TYPE,SALARY,START_CONTACT,END_CONTACT
                            FROM ( 
                                SELECT STF_ID,F_NAME,L_NAME,JOB_TYPE,SALARY,START_CONTACT,END_CONTACT, ROWNUM r
                                FROM ( 
                                    SELECT * FROM STAFF
                                    $where ORDER BY $sortname
                                    )
                                WHERE ROWNUM <= ".($start+$rp)."
                            )
                        WHERE r >= ".($start+1).")";
            $objParse = oci_parse($objConnect, $strSQL);
            oci_execute ($objParse,OCI_DEFAULT);

            $data['page'] = intval($page); // 
            $data['total'] = intval($total); //
            while ($row = oci_fetch_array($objParse,OCI_BOTH)) {
		$rows[] = array(
		"id" => $row['STF_ID'],
		"cell" => array(
			$row['STF_ID']
			,$row['F_NAME']
			,$row['L_NAME']
			,$row['JOB_TYPE']
			,$row['SALARY']
			,$row['START_CONTACT']
			,$row['END_CONTACT']
			)
		);	
            }
            $data['rows'] = $rows;
        
        }else{
            $page=0;
            $total=0;
            $rows[] = array(
				"id" => '1',
				"cell" => array(
					 '1'
					,'1'
					,'1'
					,'1'
					,'1'
					,'1'
				)
			);
            $data['page'] = intval($page); // 
            $data['total'] = intval($total);
            $data['rows'] = $rows;
        }
	echo json_encode($data);
	exit;

?>